package org.giovanolli.mumi.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.giovanolli.mumi.dao.MovementDao;
import org.giovanolli.mumi.entities.Movement;
import org.giovanolli.mumi.entities.Patient;
import org.giovanolli.mumi.entities.filter.EntityFilter;
import org.giovanolli.mumi.entities.filter.MovementFilter;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

/**
 * The Class MovementDaoImpl.
 */
@Repository("movementDao")
public class MovementDaoImpl extends HibernateDao<Movement> implements MovementDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.giovanolli.mumi.dao.impl.HibernateDao#fillCriteria(org.giovanolli.mumi.entities.filter.EntityFilter, org.hibernate.criterion.DetachedCriteria)
	 */
	@Override
	protected DetachedCriteria fillCriteria(EntityFilter<Movement> filter, DetachedCriteria criteria) {
		MovementFilter entityFilter = (MovementFilter) filter;

		if (entityFilter != null) {
			if (entityFilter.getPatient() != null) {
				criteria.add(Restrictions.eq("patient", entityFilter.getPatient()));
			}
			if (entityFilter.getMovementType() != null) {
				criteria.add(Restrictions.eq("movementType", entityFilter.getMovementType()));
			}
			if (entityFilter.getSpecialistFee() != null) {
				criteria.add(Restrictions.eq("specialistFee", entityFilter.getSpecialistFee()));
			}
			if (entityFilter.getMonth() != null) {
				criteria.add(Restrictions.eq("month", entityFilter.getMonth()));
			}
			if (entityFilter.getYear() != null) {
				criteria.add(Restrictions.eq("year", entityFilter.getYear()));
			}
		}

		return criteria;
	}

	/**
	 * Gets the balance.
	 * 
	 * @param patient
	 *            the patient
	 * @return the balance
	 */
	public List<Movement> getBalance(Patient patient) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("area", patient);

		final String query = "select u from User u where u.roleByAreas[:area] is not null";

		return find(query, paramMap);
	}
}
